﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Dns;
using Microsoft.Exchange;
using Microsoft.Exchange.WebServices;

namespace Sync_Email_to_SharePoint
{
    public partial class Config_Email : Form
    {
        string xmlfilepath;
        public Config_Email()
        {
            InitializeComponent();
        }

        private void EmailLoginbtn_Click(object sender, EventArgs e)
        {
            ExchangeService service = new ExchangeService();
            service.Credentials = new WebCredentials(EmailUserName.Text.ToString(), EmailPassword.Text.ToString());
            try
            {
                service.AutodiscoverUrl(EmailUserName.Text.ToString(), RedirectionUrlValidationCallback);
                Properties.Settings.Default.EmailName = EmailAccName.Text.ToString();
                Properties.Settings.Default.EmailUsername = EmailUserName.Text.ToString();
                Properties.Settings.Default.Emailpassword = EmailPassword.Text.ToString();
                xmlfilepath = Path.GetFullPath("UserDetails.xml");
                XDocument xmlDoc = XDocument.Load(Path.GetFullPath(xmlfilepath));
                var count = xmlDoc.Descendants("ID").Count();
                if (count < 1)
                {
                    xmlDoc.Element("UserDetails").Add(
               new XElement("User",
               new XElement("ID", 1),
               new XElement("Name", EmailAccName.Text.ToString()),
               new XElement("UserName", EmailUserName.Text.ToString()),
               new XElement("password", EmailPassword.Text.ToString()),
                new XElement("URL", ""),
               new XElement("Username", ""),
               new XElement("password", "")
               ));
                    xmlDoc.Save(xmlfilepath);
                }
                else
                {
                    List<int> li = new List<int>();
                    var IDvalue = xmlDoc.Descendants("ID");
                    foreach (var item in IDvalue)
                    {
                        li.Add(Convert.ToInt32(item.Value));
                    }
                    xmlDoc.Element("UserDetails").Add(
              new XElement("User",
              new XElement("ID", li.Max() + 1),
              new XElement("Name", EmailAccName.Text.ToString()),
              new XElement("UserName", EmailUserName.Text.ToString()),
              new XElement("password", EmailPassword.Text.ToString()),
                 new XElement("URL", ""),
               new XElement("Username", ""),
               new XElement("password", "")
              ));
                    xmlDoc.Save(xmlfilepath);
                }
                Emailnextbtn.Enabled = true;
                EmailLoginbtn.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            bool result = false;
            Uri redirectionUri = new Uri(redirectionUrl);
            if (redirectionUri.Scheme == "https")
            {
                result = true;
            }
            return result;
        }
        private void Config_Email_Load(object sender, EventArgs e)
        {
            Emailnextbtn.Enabled = false;
        }

        private void Emailnextbtn_Click(object sender, EventArgs e)
        {
            Config_Sharepoint form = new Config_Sharepoint();
            form.Show();
        }
    }

}
